//https://gitee.com/Ghobam/jsutils/raw/master/spider.js

function getRes(type) {
    switch(type){
        case "FXLAW":
            return fxLawSpider()
        case "CPWS":
            return wsLawSpider()
        case "XZCF":
            return xzcfSpider()
        case "JCWS":
            return jcwsSpider()
        case "3DM":
            return t3dmSpider()
        case "SouthPlus":
            return southplusSpider()
        case "YoutubeAll":
            return youtubeallSpider()
        case "YoutubeGame":
            return youtubegameSpider()
        case "BiliData":
            return bilidataSpider()
        case "BiliBusiness":
            return bilibusinessSpider()
        case "BiliBeLesson":
            return bilibelessonSpider()
        case "NetEaseMusic":
            return neteasemusicSpider()
    }

    return null
}

// 法信-法律法规
function fxLawSpider() {
    const res = [];
    const elems = document.querySelectorAll('ul#firstDiv li');
    if (!!elems && !!elems.length) {
        elems.forEach((e) => {
            const nameElem = e.querySelector('div.fz-title1 a')
            if (!!nameElem) {
                const name = nameElem.innerText.trim();
                const infos = e.querySelector('div.wz-info span:nth-child(1)').innerText.trim();
                const url = e.querySelector('div.fz-title1 a').href;
                const md5 = hex_md5(url);
    
                res.push({ title: `${name}(${infos})`, url, md5, type: '法信-法律法规' });
            }
        })
    }
    return res;
}

// 裁判文书
function wsLawSpider() {
    const res = [];
    const elems = document.querySelectorAll('div.LM_list');
    const filterWords = ['执行实施', '首次执行', '恢复执行', '财产保全执行'];
    if (!!elems && !!elems.length) {
        for (let i = 0; i < elems.length; i ++) {
            const e = elems[i];
            const nameElem = e.querySelector('h4 a');
            if (!nameElem) continue;
            const name = nameElem.innerText.trim();
            const type = e.querySelector('div.labelTwo').innerText.trim();
            if (!!filterWords.includes(type)) continue;
            const slfyName = e.querySelector('span.slfyName').innerText.trim();
            const no = e.querySelector('span.ah').innerText.trim();
            const date = e.querySelector('span.cprq').innerText.trim();
            const url = nameElem.href;

            res.push({ name, type, slfyName, no, date, url });
        }
    }
    return res;
}

// 行政处罚
function xzcfSpider() {
    const res = [];
    const elems = document.querySelectorAll('div.LM_list');
    if (!!elems && !!elems.length) {
        elems.forEach((e) => {
            const nameElem = e.querySelector('h4 a');
            if (!!nameElem) {
                const name = nameElem.innerText.trim();
                const person = e.querySelector('div.list_subtitle p:nth-child(1) i').innerText.trim();
                const organize = e.querySelector('div.list_subtitle p:nth-child(2) i').innerText.trim();
                const date = e.querySelector('div.list_subtitle p:nth-child(2) span:nth-child(2) i').innerText.trim();
                const url = nameElem.href;
                const md5 = hex_md5(url);
    
                res.push({ title: `【${name}】${person}(${organize} ${date})`, url, md5, type: '行政处罚文书' });
            }
        })
    }
    return res;
}

// 检察文书
function jcwsSpider() {
    const res = [];
    const elems = document.querySelectorAll('ul.list_ul li');
    if (!!elems && !!elems.length) {
        elems.forEach((e) => {
            const nameElem = e.querySelector('a');
            if (!!nameElem) {
                const name = nameElem.innerText.trim();
                const date = e.querySelector('span.date').innerText.trim();
                const url = nameElem.href;
                const md5 = hex_md5(url);
    
                res.push({ title: `${name}(${date})`, url, md5, type: '检察文书' });
            }
        })
    }
    return res;
}

// 3DM
function t3dmSpider() {
    const res = [];
    const elems = document.querySelectorAll('table#threadlisttableid tbody[id^="normalthread"] tr th');
    if (!!elems && !!elems.length) {
        elems.forEach((e) => {
            const nameElem = e.querySelector('a[class="s xst"]');
            if (!!nameElem) {
                const name = nameElem.innerText.trim();
                const url = nameElem.href;
                const md5 = hex_md5(url);
    
                res.push({ title: name, url, md5, type: '3DM' });
            }
        })
    }
    return res;
}

// SouthPlus
function southplusSpider() {
    // 要过滤掉置顶的文字
    const res = [];
    const elems = Array.from(document.querySelectorAll('div.threadlist li')).filter((e) => !e.querySelector('b font'));
    !!elems && elems.forEach((e) => {
                                    res.push({ 
                                        title: e.querySelector('a').text.trim(), 
                                        url: e.querySelector('a').href,
                                        });
                            })
    return res;
}

// YoutubeAll
function youtubeallSpider() {
    return youtubeSpider('ytd-item-section-renderer:nth-child(4) ytd-video-renderer', 100)
}

// YoutubeGame
function youtubegameSpider() {
    return youtubeSpider('ytd-grid-video-renderer', 50)
}

function youtubeSpider(selector, view) {
    const elems = document.querySelectorAll(selector);
    const res = [];

    elems.forEach((e) => {
        const scoreTxt = e.querySelector('div#metadata-line span').innerText;
        if (scoreTxt.indexOf('万') >= 0) {
            const score = scoreTxt.match(/(\d+)/)[1];
            if (score >= view) {
                const title = e.querySelector('a#video-title').title;
                const author = e.querySelector('div.ytd-channel-name yt-formatted-string').title;
                const url = e.querySelector('a#video-title').href.split('&')[0];
                const content = '<img src="https://i.ytimg.com/vi/' + url.match(/\?v=(.*)/)[1] + '/hqdefault.jpg"/>';
                res.push({
                    title: `【${author}】${title}`,
                    url,
                    content,
                })
            }
        }
    })
    return res;
}

function bilibusinessSpider() {
    const elems = document.querySelectorAll('div.content-card-outside a');
    const res = [];

    elems.forEach((e) => {
        const title = e.querySelector('img').title;
        const url = e.href;
        const content = '<img src="' + e.querySelector('img').src + '"/>';
        res.push({
            title,
            url,
            content,
        })
    })
    return res;
}

function bilibelessonSpider() {
    const elems = document.querySelectorAll('ul.lf_list_anli li a');
    const res = [];

    elems.forEach((e) => {
        const title = e.querySelector('strong.title').innerText;
        const url = e.href;
        const content = '<img src="' + e.querySelector('img').src + '"/>';
        res.push({
            title,
            url,
            content,
        })
    })
    return res;
}

function bilidataSpider() {
    const infos = document.querySelector('meta[name="description"]').content;
    const res = [{
        "标题": document.querySelector('div.video-info-title-inner h1').title,
        "作者": document.querySelector('meta[name="author"]').content,
        "投稿时间": document.querySelector('meta[itemprop="datePublished"]').content,
        "抓取时间": getCurrentDate(),
        "播放量": infos.match(/视频播放量 (\d+)/)[1],
        "点赞量": infos.match(/点赞数 (\d+)/)[1],
        "投币量": infos.match(/投硬币枚数 (\d+)/)[1],
        "收藏量": infos.match(/收藏人数 (\d+)/)[1],
        "转发量": infos.match(/转发人数 (\d+)/)[1],
        "弹幕量": infos.match(/弹幕量 (\d+)/)[1],
        "正在看": document.querySelector('div.bpx-player-video-info-online b').innerText.trim(),
        "评论数": document.querySelector('bili-comments').shadowRoot.querySelector('bili-comments-header-renderer').shadowRoot.querySelector('div#count').innerText,
        "视频链接": document.location.href.split('?')[0],
    }]
    
    return res;
}

function neteasemusicSpider() {
    const elems = document.querySelector('ul.m-cvrlst').querySelectorAll('li');
    const res = [];

    elems.forEach((e) => {
        const title = e.querySelector('p.dec a').innerText + ' - ' + e.querySelector('p.f-thide a').innerText;
        const url = e.querySelector('p.dec a').href;
        const content = '<img src="' + e.querySelector('div.u-cover img').src + '"/>';
        res.push({
            title,
            url,
            content,
        })
    })
    return res;
}

function getCurrentDate(timestamp) {
    let date = timestamp ? new Date(timestamp * 1000) : new Date();
    let month = getFormattedTime(date.getMonth() + 1);
    let day = getFormattedTime(date.getDate());
    let hours = getFormattedTime(date.getHours());
    let minutes = getFormattedTime(date.getMinutes());
    let second = getFormattedTime(date.getSeconds());
    return `${date.getFullYear()}-${month}-${day} ${hours}:${minutes}:${second}`
}

function getFormattedTime(s) {
    return String(s).padStart(2, '0');
}